#!/usr/bin/python
# coding=utf8
#
# Copyright (C) 2011 XNData contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys, os
sys.path.append (os.getcwd () + '/../../')
import xndata.client
import xndata.client.note
import unittest
import datetime

class XNNoteClientTestCase (unittest.TestCase):

    def setUp (self):
        self.client = xndata.client.note.XNNoteClient (uname="zhangchitc@gmail.com", upass="aaaaaaaa")


    def test_get_total_note (self):
        # Ren su yi's note
        self.assertTrue ( self.client.getTotalNote ("247780420") >= 14)
        # My note
        self.assertTrue (self.client.getTotalNote ("242124580") >= 145)

    def test_get_total_note_list (self):
        self.assertTrue (self.client.getTotalNoteList ("247780420") >= 2)
        self.assertTrue (self.client.getTotalNoteList ("242124580") >= 15)

    def test_get_all_note (self):
        nlist = self.client.getAllNote ("247780420")
        last = len (nlist) - 1
        self.assertEqual (nlist[last].note_id, "308359235")
        self.assertEqual (nlist[last].author_id, "247780420")
        self.assertEqual (nlist[last].title, "申明")
        self.assertEqual (nlist[last].pub_time, datetime.datetime (2008, 7, 12, 19, 7, 0))
        
        nlist = self.client.getAllNote ("242124580")
        last = len (nlist) - 1
        self.assertEqual (nlist[last].note_id, "292553624")
        self.assertEqual (nlist[last].author_id, "242124580")
        self.assertEqual (nlist[last].title, "很窘啊")
        self.assertEqual (nlist[last].pub_time, datetime.datetime (2008, 5, 13, 3, 47, 0))

    def test_get_note (self):
        note = self.client.getNote ("247780420", "439115437")
        self.assertEqual (note.author_id, "247780420")
        self.assertEqual (note.note_id, "439115437")
        self.assertEqual (note.title, "我是那杯具里翻滚的小茶叶")
        self.assertEqual (note.pub_time, datetime.datetime (2010, 1, 3, 0, 39, 0))
        self.assertTrue (note.content.startswith ('<div id="blogContent" class="text-article">'))

        note = self.client.getNote ("242124580", "737669617")
        self.assertEqual (note.author_id, "242124580")
        self.assertEqual (note.note_id, "737669617")
        self.assertEqual (note.title, "再请教一个北大信息系统的问题")
        self.assertEqual (note.pub_time, datetime.datetime (2011, 7, 10, 16, 47, 0))
        self.assertTrue (note.content.startswith ('<div id="blogContent" class="text-article">'))

suite1 = unittest.TestLoader().loadTestsFromTestCase (XNNoteClientTestCase)
allsuite = unittest.TestSuite ([suite1])

unittest.TextTestRunner(verbosity=2).run(allsuite)

